<?php
/**
 * 经纪人跟进模型
 */

namespace app\badmin\model;


use app\common\model\BaseModel;

class AgentFollowLog extends BaseModel {

    /**
     * 跟进记录
     * @param $params
     * @return array
     */
    public static function follow_log($params) {
        $column = ['remark as type,itime'];
        $map[] = ['agent_user_id', '=', $params['id']];
        $order = ['itime' => 'desc'];
        $data = self::getDatas($map, $column, $order);
        if (empty($data)) {
            $result['count'] = 0;
            $result['page'] = 1;
            $result['pageNum'] = 1;
            $result['data'] = [];
        } else {
            foreach ($data as $k => $v) {
                $data[$k]['day_time'] = date('Y-m-d', $v['itime']);
                $data[$k]['itime'] = date('H:i:s', $v['itime']);
            }
            $data = self::treeFilter($data);
            $count = count($data);
            $page3 = (int)$params['page3'];
            $long3 = (int)$params['long3'];
            $page3_ = ($page3 - 1) * $long3;
            $data = array_slice($data, $page3_, $long3);
            if ($long3 > 0) {
                $pageNum = ceil($count / $long3);
            } else {
                $pageNum = 1;
            }
            $result['count'] = $count;
            $result['page'] = $page3;
            $result['pageNum'] = $pageNum;
            $result['data'] = $data;
        }
        return $result;
    }

    /**
     * 结构
     * @param $data
     * @return array
     */
    public static function treeFilter($data) {
        $tree = [];
        foreach ($data as &$val) {
            $arr = [
                'itime' => $val['itime'],
                'type'  => base64_decode($val['type']),
            ];
            $tree[$val['itime']]['day_time'] = $val['day_time'];
            $tree[$val['itime']]['children'][] = $arr;
        }
        return array_values($tree);
    }

    /**
     * 筛选多条
     * @param $map
     * @param $column
     * @param $page
     * @param $long
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getDatas($map, $column, $order) {
        return db("agent_follow_log")
            ->where($map)
            ->order($order)
            ->field($column)
            ->select();
    }

}